
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
<div class="archive_info">
<h1>JavaScript <span class="color_h1">对象</span>
</h1>
<hr/>
<p class="intro">JavaScript 中的所有事物都是对象：字符串、数值、数组、函数...
</p>
<p class="intro">此外，JavaScript 允许自定义对象。</p>
<hr/>
<h2>所有事物都是对象</h2>
<p>JavaScript 提供多个内建对象，比如 String、Date、Array 等等。
对象只是带有属性和方法的特殊数据类型。</p>
<ul>
<li>布尔型可以是一个对象。</li>
<li>数字型可以是一个对象。</li>
<li>字符串也可以是一个对象</li>
<li>日期是一个对象</li>
<li>数学和正则表达式也是对象</li>
<li>数组是一个对象</li>
<li>甚至函数也可以是对象</li>
</ul>
<hr/>
<h2>JavaScript 对象</h2>
<p>对象只是一种特殊的数据。对象拥有<strong>属性</strong>和<strong>方法</strong>。</p>
<hr/>
<h2>访问对象的属性</h2>
<p>属性是与对象相关的值。</p>
<p>访问对象属性的语法是：</p>
<pre>objectName.propertyName</pre>
<p>这个例子使用了 String 对象的 length 属性来获得字符串的长度：</p>
<pre>var message="Hello World!";
var x=message.length;</pre>
<p>在以上代码执行后，x 的值将是：</p>
<pre>12</pre>
<br/><hr/>
<h2>访问对象的方法</h2>
<p>方法是能够在对象上执行的动作。</p>
<p>您可以通过以下语法来调用方法：</p>
<pre>objectName.methodName()</pre>
<p>这个例子使用了 String 对象的 toUpperCase() 方法来将文本转换为大写：</p>
<pre>var message="Hello world!";
var x=message.toUpperCase();</pre>
<p>在以上代码执行后，x 的值将是：</p>
<pre>HELLO WORLD!</pre>
<br/><hr/>
<h2>创建 JavaScript 对象</h2>
<p>通过 JavaScript，您能够定义并创建自己的对象。</p>
<p>创建新对象有两种不同的方法：</p>
<ul>
<li>使用 Object 定义并创建对象的实例。</li>
<li>使用函数来定义对象，然后创建新的对象实例。</li>
</ul>
<h3>使用 Object</h3>
<p>在 JavaScript 中，几乎所有的对象都是 Object 类型的实例，它们都会从 Object.prototype 继承属性和方法。</p>
<p>Object 构造函数创建一个对象包装器。</p><p>
Object 构造函数，会根据给定的参数创建对象，具体有以下情况：</p>
<ul><li>
如果给定值是 null 或 undefined，将会创建并返回一个空对象。</li><li>
如果传进去的是一个基本类型的值，则会构造其包装类型的对象。</li><li>
如果传进去的是引用类型的值，仍然会返回这个值，经他们复制的变量保有和源对象相同的引用地址。</li><li>
当以非构造函数形式被调用时，Object 的行为等同于 new Object()。</li></ul>
<p>语法格式：</p><pre>
// 以构造函数形式来调用
new Object([value])</pre>
<p>value 可以是任何值。</p>
<p>以下实例使用 Object 生成布尔对象：</p>
<pre>// 等价于 o = new Boolean(true);
var o = new Object(true);</pre>
<p>这个例子创建了对象的一个新实例，并向其添加了四个属性：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-identifier">person</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Object</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-identifier">person</span><span class="hl-code">.</span><span class="hl-identifier">firstname</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">John</span><span class="hl-quotes">"</span><span class="hl-code">;
</span><span class="hl-identifier">person</span><span class="hl-code">.</span><span class="hl-identifier">lastname</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">Doe</span><span class="hl-quotes">"</span><span class="hl-code">;
</span><span class="hl-identifier">person</span><span class="hl-code">.</span><span class="hl-identifier">age</span><span class="hl-code">=</span><span class="hl-number">50</span><span class="hl-code">;
</span><span class="hl-identifier">person</span><span class="hl-code">.</span><span class="hl-identifier">eyecolor</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">blue</span><span class="hl-quotes">"</span><span class="hl-code">;</span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener noreferrer" target="_blank">尝试一下 »</a>
</div>
<p>也可以使用对象字面量来创建对象，语法格式如下：</p>
<pre>{ name1 : value1, name2 : value2,...nameN : valueN }</pre>
<p>其实就是大括号里面创建 <strong>name:valu</strong>e 对，然后 <strong>name:value</strong> 对之间以逗号 <span class="marked">,</span> 隔开。</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};
</div>
<br/><a class="tryitbtn"  rel="noopener noreferrer" target="_blank">尝试一下 »</a>
</div> <p>JavaScript 对象就是一个 <strong>name:value</strong> 集合。</p>
<br/><hr/>
<h2>使用对象构造器</h2>
<p>本例使用函数来构造对象：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">person</span><span class="hl-brackets">(</span><span class="hl-identifier">firstname</span><span class="hl-code">,</span><span class="hl-identifier">lastname</span><span class="hl-code">,</span><span class="hl-identifier">age</span><span class="hl-code">,</span><span class="hl-identifier">eyecolor</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">firstname</span><span class="hl-code">=</span><span class="hl-identifier">firstname</span><span class="hl-code">;
    </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">lastname</span><span class="hl-code">=</span><span class="hl-identifier">lastname</span><span class="hl-code">;
    </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">age</span><span class="hl-code">=</span><span class="hl-identifier">age</span><span class="hl-code">;
    </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">eyecolor</span><span class="hl-code">=</span><span class="hl-identifier">eyecolor</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener noreferrer" target="_blank">尝试一下 »</a>
</div>
<p>在JavaScript中，this通常指向的是我们正在执行的函数本身，或者是指向该函数所属的对象（运行时）</p>
<hr/>
<h2>创建 JavaScript 对象实例</h2>
<p>一旦您有了对象构造器，就可以创建新的对象实例，就像这样：</p>
<pre>var myFather=new person("John","Doe",50,"blue");
var myMother=new person("Sally","Rally",48,"green");</pre>
<br/><hr/>
<h2>把属性添加到 JavaScript 对象</h2>
<p>您可以通过为对象赋值，向已有对象添加新属性：</p>
<p>假设 person 对象已存在 - 您可以为其添加这些新属性：firstname、lastname、age 以及 eyecolor：</p>
<pre>person.firstname="John";
person.lastname="Doe";
person.age=30;
person.eyecolor="blue";

x=person.firstname;</pre>
<p>在以上代码执行后，x 的值将是：</p>
<pre>John</pre>
<br/><hr/>
<h2>把方法添加到 JavaScript 对象</h2>
<p>方法只不过是附加在对象上的函数。</p>
<p>在构造器函数内部定义对象的方法：</p>
<pre>
function person(firstname,lastname,age,eyecolor)
{
	this.firstname=firstname;
	this.lastname=lastname;
	this.age=age;
	this.eyecolor=eyecolor;

	this.changeName=changeName;
	function changeName(name)
	{
		this.lastname=name;
	}
}
</pre>
<p>changeName() 函数 name 的值赋给 person 的 lastname 属性。
</p>
<div class="example">
<h2 class="example">现在您可以试一下：</h2>
<div class="example_code notranslate">

myMother.changeName("Doe");</div>
<br/><a class="tryitbtn"  rel="noopener noreferrer" target="_blank">尝试一下 »</a>
</div>
<hr/>
<h2>JavaScript 类</h2>
<p>JavaScript 是面向对象的语言，但 JavaScript 不使用类。</p>
<p>在 JavaScript 中，不会创建类，也不会通过类来创建对象（就像在其他面向对象的语言中那样）。</p>
<p>JavaScript 基于 prototype，而不是基于类的。</p>
<hr/>
<h2>JavaScript for...in 循环</h2>
<p>JavaScript for...in 语句循环遍历对象的属性。</p>
<h3>语法</h3>
<pre>
for (variable in object)
{
	执行的代码……
}
</pre>
<p><b>注意： </b>for...in 循环中的代码块将针对每个属性执行一次。</p>
<h3>实例</h3>
<p>循环遍历对象的属性：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">person</span><span class="hl-code">=</span><span class="hl-brackets">{</span><span class="hl-identifier">fname</span><span class="hl-code">:</span><span class="hl-quotes">"</span><span class="hl-string">John</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">lname</span><span class="hl-code">:</span><span class="hl-quotes">"</span><span class="hl-string">Doe</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-identifier">age</span><span class="hl-code">:</span><span class="hl-number">25</span><span class="hl-brackets">}</span><span class="hl-code">; 
 
</span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">x</span><span class="hl-code"> </span><span class="hl-reserved">in</span><span class="hl-code"> </span><span class="hl-identifier">person</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">txt</span><span class="hl-code">=</span><span class="hl-identifier">txt</span><span class="hl-code"> + </span><span class="hl-identifier">person</span><span class="hl-brackets">[</span><span class="hl-identifier">x</span><span class="hl-brackets">]</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
<br/>
<a class="tryitbtn"  rel="noopener noreferrer" target="_blank">尝试一下 »</a>
</div>
<br/><hr/>
<h2>JavaScript 的对象是可变的</h2>
<p>
对象是可变的，它们是通过引用来传递的。</p>
<p>
以下实例的 person 对象不会创建副本：</p>
<pre>var x = person;  // 不会创建 person 的副本，是引用</pre>
<p>如果修改 x ，person 的属性也会改变：</p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code"><div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">person</span><span class="hl-code"> = </span><span class="hl-brackets">{</span><span class="hl-identifier">firstName</span><span class="hl-code">:</span><span class="hl-quotes">"</span><span class="hl-string">John</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-identifier">lastName</span><span class="hl-code">:</span><span class="hl-quotes">"</span><span class="hl-string">Doe</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-identifier">age</span><span class="hl-code">:</span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-identifier">eyeColor</span><span class="hl-code">:</span><span class="hl-quotes">"</span><span class="hl-string">blue</span><span class="hl-quotes">"</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-identifier">person</span><span class="hl-code">;
</span><span class="hl-identifier">x</span><span class="hl-code">.</span><span class="hl-identifier">age</span><span class="hl-code"> = </span><span class="hl-number">10</span><span class="hl-code">;           </span><span class="hl-comment">//</span><span class="hl-comment">  x.age 和 person.age 都会改变</span><span class="hl-comment"></span></div></div><br/>
<a class="tryitbtn"  rel="noopener noreferrer" target="_blank">尝试一下 »</a></div> <div class="scroll-top"><a >返回顶部</a></div>
</div>

    </body>
    </html>
    